using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using Random = UnityEngine.Random;

public class GameManager : MonoBehaviour
{
    public GameObject playerPrefab;
    public GameObject coinPrefab;
    public int coinCount = 4;
    void Start()
    {
        //CreatePlayer();
        NetworkManager.Singleton.OnClientConnectedCallback += (id) =>
        {
            Debug.Log("A new Client connected,id="+id);
        };
        NetworkManager.Singleton.OnClientDisconnectCallback += (id) =>
        {
            Debug.Log("A Client disconnet,id="+id);
        };
        NetworkManager.Singleton.OnServerStarted += () =>
        {
            Debug.Log("Server started success");
            //创建金币由服务器进行。
            CreateCoins();
        };
    }

    private void CreatePlayer()
    {
        Instantiate(playerPrefab, new Vector3(Random.Range(-5,5), 0.5f,Random.Range(-5,5)), Quaternion.identity);
    }
    private void CreateCoins()
    {
        for (int i = 0; i < coinCount; i++)
        {
            GameObject ob = Instantiate(coinPrefab, new Vector3(Random.Range(-10, 10), 5f, Random.Range(-10, 10)), Quaternion.identity);
            //服务器生成
            ob.GetComponent<NetworkObject>().Spawn();
        }
    }
    public void OnStartServerBtnClick()
    {
        if (NetworkManager.Singleton.StartServer())
        {
            Debug.Log("StartServer success");
        }
        else
        {
            Debug.Log("Server failed to start");
        }
    }
    public void OnStartClientBtnClick()
    {
        if (NetworkManager.Singleton.StartClient())
        {
            Debug.Log("StartClient success");
        }
        else
        {
            Debug.Log("Server failed to start");
        }
    }
    //host就是server+client都是本机
    public void OnStartHostBtnClick()
    {
        if (NetworkManager.Singleton.StartHost())
        {
            Debug.Log("StartHost success");
        }
        else
        {
            Debug.Log("Server failed to start");
        }
    }
    public void OnShutdownNetworkBtnClick()
    {
        NetworkManager.Singleton.Shutdown();
        Debug.Log("Network Shutdown");
    }
}
